package com.huike.controller.common;

import com.huike.result.Result;
import com.huike.utils.FtpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/common")
public class UploadController {

    @PostMapping("/upload")
    public Result upload(@RequestPart("file") MultipartFile file) throws Exception {


        //获取原始文件名
        String originalFilename = file.getOriginalFilename();

        //构建新的文件名
        String extname = originalFilename.substring(originalFilename.lastIndexOf("."));//文件扩展名
        String newFileName = UUID.randomUUID().toString() + extname;//随机名+文件扩展名

        //将文件存储在服务器的磁盘目录
        FtpUtils.sshSftp(file.getBytes(), newFileName);

        return Result.success();
    }
}